package com.sunhy.accountserver.mapper;

import com.sunhy.accountserver.entity.AccountEntity;
import org.apache.ibatis.annotations.*;

import java.math.BigDecimal;
import java.util.List;

@Mapper
public interface AccountMapper {

    @Select("SELECT `user_id`, `username`, `account_balance`, `status` FROM `account`")
    List<AccountEntity> getAll();

    @Select("SELECT `user_id`, `username`, `account_balance`, `status` FROM `account` WHERE `user_id` = #{userId}")
    AccountEntity getByUserId(@Param("userId") Integer userId);

    @Insert("INSERT INTO `account` (`user_id`, `username`, `account_balance`, `status`) VALUES (#{userId}, #{username}, #{accountBalance}, 1)")
    int insert(AccountEntity accountEntity);

    @Update("UPDATE `account` SET `account_balance`=`account_balance` + #{amount} WHERE `user_id` = #{userId} ")
    int updateAddAmountAndStatus( @Param("userId") int userId, @Param("amount") BigDecimal amount);

    @Update("UPDATE `account` SET `account_balance`=`account_balance` - #{amount} WHERE `user_id` = #{userId} ")
    int updateSubAmountAndStatus( @Param("userId") int userId, @Param("amount") BigDecimal amount);

}
